package com.sky.zzh;

import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;

import com.next.intf.ITaskListener;
import com.next.net.SHGet;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.sweetdialog.SweetDialog;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;

public class TestActivity extends Activity implements ITaskListener {
  private SHGet turnUpTask;
  private boolean flag = false;
  private String fromDate;
  private String endDate;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fromDate = "20150923";
    endDate = "20150924";
    turnUpTask = new SHGet();
    turnUpTask.setListener(this);
    if (flag) {
      turnUpTask.setUrl(ConfigDefinition.URL + "app/SensorDataToday");
       turnUpTask.getTaskArgs().put("UUID", UserInfoManager.getInstance().getUUID());
//      turnUpTask.getTaskArgs().put("UUID", "MT003");
      turnUpTask.getTaskArgs().put("UserId", UserInfoManager.getInstance().getUserId());
      turnUpTask.getTaskArgs().put("DataProperty", "1");
    } else {
      StringBuffer sb=new StringBuffer();
      sb.append(ConfigDefinition.URL);
      sb.append("app/SensorDataHistory?UUID=");
      //sb.append(UserInfoManager.getInstance().getUUID());
      sb.append("MT003");
      sb.append("&");
      sb.append("DataProperty=1&FromDate=");
      sb.append(fromDate);
      sb.append("&");
      sb.append("EndDate=");
      sb.append(endDate);
      turnUpTask.setUrl(sb.toString());
      // turnUpTask.setUrl(ConfigDefinition.URL + "app/SensorDataHistory");
      // turnUpTask.getTaskArgs().put("UUID", UserInfoManager.getInstance().getUUID());
      // turnUpTask.getTaskArgs().put("UUID", "MT003");
      // turnUpTask.getTaskArgs().put("UserId", UserInfoManager.getInstance().getUserId());
      // turnUpTask.getTaskArgs().put("DataProperty", "1");
      // turnUpTask.getTaskArgs().put("FromDate", URLEncoder.encode(fromDate));
      // turnUpTask.getTaskArgs().put("EndDate", URLEncoder.encode(endDate));
      // turnUpTask.getTaskArgs().put("FromTime", URLEncoder.encode(""));
      // turnUpTask.getTaskArgs().put("EndTime", URLEncoder.encode(""));
    }
    turnUpTask.start();

  }


  @Override
  public void onTaskFinished(SHTask task) throws Exception {
    JSONObject json = new JSONObject(task.getResult().toString());
    
    if ("200".equals(json.optString("ReturnCode"))) {
      try {
        // 传感器的数据
        JSONArray sensorDataJSONArray = json.optJSONArray("SensorData");
        JSONArray array = null;
        JSONObject obj = null;
        // 处理property为1的体动
        for (int i = 0; i < sensorDataJSONArray.length(); i++) {
          array = sensorDataJSONArray.getJSONObject(i).optJSONArray("Data");
          for (int j = 0; j < array.length(); j++) {
            obj = array.getJSONObject(j);
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    } else {
      SHDialog.dismissProgressDiaolg();
      new SweetDialog(this, SweetDialog.ERROR_TYPE).setTitleText("提示TurnUp")
          .setContentText(json.optString("ErrorMessage")).show();
    }
  }


  @Override
  public void onTaskFailed(SHTask task) {
    // TODO Auto-generated method stub

  }


  @Override
  public void onTaskUpdateProgress(SHTask task, int count, int total) {
    // TODO Auto-generated method stub

  }


  @Override
  public void onTaskTry(SHTask task) {
    // TODO Auto-generated method stub

  }


}
